package com.duleme.servlet;

import com.duleme.common.EncodeUtils;
import com.duleme.dao.UserDao;
import com.duleme.entity.Captcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "AlterPwdServlet",urlPatterns = "/alter/password1")
public class AlterPwdServlet extends HttpServlet {
    UserDao userDao = new UserDao();

    public void  check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userAccount = request.getParameter("userAccount");
        if(!userDao.checkAccountValid(userAccount)){
            PrintWriter writer = response.getWriter();
            String captcha = request.getParameter("captcha");//获取客户端的验证码输入框
            Captcha checkCaptcha = (Captcha)request.getSession().getAttribute("phoneCaptcha");//获取服务器发往session的验证码
            if (checkCaptcha == null){//没有获取到session的验证码
                writer.print(2);
                System.out.println("没有获取到session的captcha");
                writer.close();
            }else{
                System.out.println("手机号："+userAccount);
                System.out.println("输入验证码："+captcha);
                System.out.println("session的验证码"+checkCaptcha.getCode());
                long nh = 1000 * 60 * 60;
                if (!userAccount.equals(checkCaptcha.getUserAccount())){
                    System.out.println("账号不匹配");
                    writer.print(1);//账号不匹配
                    writer.close();
                }else {
                    if (!captcha.equals(checkCaptcha.getCode())){
                        writer.print(2);//验证码不匹配
                        System.out.println("验证码与session不一致");
                        writer.close();
                    }
                    else {
                        if (Math.abs(new Date().getTime()-checkCaptcha.getCreateTime().getTime())/nh>checkCaptcha.getExpire()){
                            writer.print(3);//超时
                            System.out.println("超时");
                            writer.close();
                        }
                        else {
                            writer.print(0);
                            System.out.println("success");
                            writer.close();
                        }
                    }
                }
            }
        }
        else {
            response.getWriter().print(4);
            System.out.println("此账号没有注册");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        check(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
